﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OpenMVCRM.ViewModels;
using OpenMVCRM.Entities;
using OpenMVCRM.DataAccess;
using OpenMVCRM.Mapping;
using OpenMVCRM.Services;
namespace OpenMVCRM.Controllers
{
    public class AccountController : Controller
    {
        private IRepositary repo;

        public AccountController()
        {
            repo = new Repositary();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Register()
        {
            var regViewModel = new RegisterViewModel();

            return View(regViewModel);
        }
        [HttpPost]
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User();
                user.FirstName = model.Name;
                user.EmailAddress = model.EmailAddress;

                user.Salt = Security.GetSalt(model.EmailAddress.Length);
                user.Password = Security.CreatePasswordHash(model.Password, user.Salt);

                var result = repo.InsertUser(user);
                if (result.Status)
                    return RedirectToAction("Welcome");
                else
                    ModelState.AddModelError("", result.Message);
            }
            return View(model);
        }
        public ActionResult Welcome()
        {

            return View();
        }

    }
}
